package org.jeecg.modules.room.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.constant.FillRuleConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.modules.room.entity.*;
import org.jeecg.modules.room.mapper.RoomRegisterInfoMxMapper;
import org.jeecg.modules.room.mapper.RoomRegisterInfoMapper;
import org.jeecg.modules.room.service.*;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 登记单信息主表
 * @Author: jeecg-boot
 * @Date:   2024-01-08
 * @Version: V1.0
 */
@Service
public class RoomRegisterInfoServiceImpl extends ServiceImpl<RoomRegisterInfoMapper, RoomRegisterInfo> implements IRoomRegisterInfoService {

	@Autowired
	private RoomRegisterInfoMapper roomRegisterInfoMapper;
	@Autowired
	private RoomRegisterInfoMxMapper roomRegisterInfoMxMapper;
	@Autowired
	private IRoomInfoService roomInfoService;
	@Autowired
	private IRoomTypeService roomTypeService;
	@Autowired
	private IRoomExpenseInfoService roomExpenseInfoService;
	@Autowired
	private IRoomBookInfoService roomBookInfoService;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(RoomRegisterInfo roomRegisterInfo, List<RoomRegisterInfoMx> roomRegisterInfoMxList) {

		try {
			JSONObject formData = new JSONObject();
			formData.put("prefix", "DJ");
			roomRegisterInfo.setAuditFlag("0"); //夜审标记
			roomRegisterInfo.setRegisterStatus("00");//登记单状态在店
			roomRegisterInfo.setRoomRegisterCode((String) FillRuleUtil.executeRule(FillRuleConstant.ORDER, formData));
			roomRegisterInfoMapper.insert(roomRegisterInfo);
			if (roomRegisterInfoMxList == null || roomRegisterInfoMxList.size() == 0) {
				throw new JeecgBootException("登记单明细信息不能为空!");
			} else {
				for (RoomRegisterInfoMx entity : roomRegisterInfoMxList) {
					//外键设置
					int usableNum = roomInfoService.selectUsableRoomNum(entity.getRoomTypeCode(), roomRegisterInfo.getDateArrival(), roomRegisterInfo.getDateDeparture());
					if (usableNum < entity.getRoomNum()) {
						RoomType roomType = roomTypeService.getOne(new QueryWrapper<RoomType>().eq("room_type_code", entity.getRoomTypeCode()));
						throw new JeecgBootException(roomRegisterInfo.getDateArrival() + "至" + roomRegisterInfo.getDateDeparture() + roomType.getRoomTypeName() + "房间余量不足，最大可用房间数" + usableNum);
					}
					entity.setRoomRegisterCode(roomRegisterInfo.getRoomRegisterCode());
					roomRegisterInfoMxMapper.insert(entity);
				}
			}
			//如果为预定转入住，修改预订单状态、定金转押金
			if(StrUtil.isNotBlank(roomRegisterInfo.getBookCode())) {
				RoomBookInfo roomBookInfo = new RoomBookInfo();
				roomBookInfo.setBookStatus("30");
				roomBookInfoService.update(new QueryWrapper<RoomBookInfo>().eq("book_code", roomRegisterInfo.getBookCode()));

				//todo 修改预订单状态、定金转押金

			}
		}
		catch (Exception e){
			throw e;
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(RoomRegisterInfo roomRegisterInfo,List<RoomRegisterInfoMx> roomRegisterInfoMxList) {

		try {
			roomRegisterInfoMapper.updateById(roomRegisterInfo);

			//1.先删除子表数据
			roomRegisterInfoMxMapper.deleteByMainId(roomRegisterInfo.getId());

			if (roomRegisterInfoMxList == null || roomRegisterInfoMxList.size() == 0) {
				throw new JeecgBootException("登记单明细信息不能为空!");
			}
			//2.子表数据重新插入
			else {
				for (RoomRegisterInfoMx entity : roomRegisterInfoMxList) {
					//外键设置
					int usableNum = roomInfoService.selectUsableRoomNum(entity.getRoomTypeCode(), roomRegisterInfo.getDateArrival(), roomRegisterInfo.getDateDeparture());
					if (usableNum < entity.getRoomNum()) {
						RoomType roomType = roomTypeService.getOne(new QueryWrapper<RoomType>().eq("room_type_code", entity.getRoomTypeCode()));
						throw new JeecgBootException(roomRegisterInfo.getDateArrival() + "至" + roomRegisterInfo.getDateDeparture() + roomType.getRoomTypeName() + "房间余量不足，最大可用房间数" + usableNum);
					}
					entity.setRoomRegisterCode(roomRegisterInfo.getRoomRegisterCode());
					roomRegisterInfoMxMapper.insert(entity);
				}
			}
		}
		catch (Exception e){
			throw e;
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {

		RoomRegisterInfo roomRegisterInfo = this.baseMapper.selectById(id);

		//查询是否完成退款
		QueryWrapper<RoomExpenseInfo> queryWrapper = new  QueryWrapper<RoomExpenseInfo>();
		queryWrapper.select("sum(amount) as amount");
		queryWrapper.eq("business_code",roomRegisterInfo.getRoomRegisterCode());
		RoomExpenseInfo roomExpenseInfo = roomExpenseInfoService.getOne(queryWrapper);

		if (!roomRegisterInfo.getAuditFlag().equals("1")) {
			throw new JeecgBootException("登记单号:" + roomRegisterInfo.getRoomRegisterCode() + "已夜审不可删除!");
		}
		else if(roomExpenseInfo != null && roomExpenseInfo.getAmount() != null && roomExpenseInfo.getAmount() != 0){
			throw new JeecgBootException("登记单号:" + roomRegisterInfo.getRoomRegisterCode() + "未完成退款不可删除!");
		}
		else{
			roomRegisterInfoMxMapper.deleteByMainId(id);
			roomRegisterInfoMapper.deleteById(id);

			//删除收款
			roomExpenseInfoService.remove(new QueryWrapper<RoomExpenseInfo>().eq("business_code",roomExpenseInfo.getBusinessCode()));
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			RoomRegisterInfo roomRegisterInfo = this.baseMapper.selectById(id);

			//查询是否完成退款
			QueryWrapper<RoomExpenseInfo> queryWrapper = new  QueryWrapper<RoomExpenseInfo>();
			queryWrapper.select("sum(amount) as amount");
			queryWrapper.eq("business_code",roomRegisterInfo.getRoomRegisterCode());
			RoomExpenseInfo roomExpenseInfo = roomExpenseInfoService.getOne(queryWrapper);

			if (!roomRegisterInfo.getAuditFlag().equals("1")) {
				throw new JeecgBootException("登记单号:" + roomRegisterInfo.getRoomRegisterCode() + "已夜审不可删除!");
			}
			else if(roomExpenseInfo != null && roomExpenseInfo.getAmount() != null && roomExpenseInfo.getAmount() != 0){
				throw new JeecgBootException("登记单号:" + roomRegisterInfo.getRoomRegisterCode() + "未完成退款不可删除!");
			}
			else{
				roomRegisterInfoMxMapper.deleteByMainId(id.toString());
				roomRegisterInfoMapper.deleteById(id);

				//删除收款
				roomExpenseInfoService.remove(new QueryWrapper<RoomExpenseInfo>().eq("business_code",roomExpenseInfo.getBusinessCode()));
			}
		}
	}
	
}
